package gui.customizer.panes.dhcp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;
import core.models.DhcpConfiguration;
import core.models.DhcpSubnet;

public class Subnets extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2748257421883637588L;
	private GeneralOptions generalOptions;
	private JList listSubnets;
	private JButton btnAdd;
	private JButton btnDelete;
	private SubnetEditor subnetEditor;
	private DhcpConfiguration configuration;

	/**
	 * Create the panel.
	 */
	public Subnets(DhcpConfiguration conf, SaveAndSend saveAndSend) {
		setLayout(new MigLayout("", "[grow][grow][]", "[][][][grow][grow]"));
		
		this.configuration = conf;
		
		// General options
		generalOptions = new GeneralOptions();
		add(generalOptions, "cell 0 0,grow");
		
		// Save and send
		add(saveAndSend, "cell 1 0 2 1,grow");
		
		// List and buttons
		listSubnets = new JList();
		listSubnets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listSubnets.setLayoutOrientation(JList.VERTICAL);
		
		JScrollPane subnetsScroller = new JScrollPane(listSubnets);
		subnetsScroller.setBorder(BorderFactory.createTitledBorder("Subnets"));
		add(subnetsScroller, "cell 0 1 2 3,grow");
		
		btnAdd = new JButton("Add");
		add(btnAdd, "cell 2 1,growx");
		
		btnDelete = new JButton("Delete");
		add(btnDelete, "cell 2 2,growx");
		
			
		// Subnet editor
		subnetEditor = new SubnetEditor();
		JScrollPane subnetEditorScroller = new JScrollPane(subnetEditor);
		subnetEditorScroller.setBorder(BorderFactory.createTitledBorder("Edit Subnet"));
		add(subnetEditorScroller, "cell 0 4 3 1,grow");
		
		// Initialize
		try {
			initialize();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void initialize() throws IOException {
		// General options
		generalOptions.setGeneralOptions(configuration.getOptions());
		
		// List
		listSubnets.setModel(configuration);
		listSubnets.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting())
					return;
				DhcpSubnet subnet = (DhcpSubnet) listSubnets.getSelectedValue();
				if (subnet != null) {
					subnetEditor.setSubnet(subnet);
				} else {
					subnetEditor.hideApplyButton();
				}
			}
		});
		
		// Buttons
		btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DhcpSubnet subnet = subnetEditor.getSubnet();
				configuration.addElement(subnet);
				listSubnets.setSelectedValue(subnet, true);
			}
		});
		
		btnDelete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (listSubnets.getSelectedIndex() != -1) {
					int index = listSubnets.getSelectedIndex();
					configuration.remove(index);
					if (index == configuration.getSize())
						index--;
					listSubnets.setSelectedIndex(index);
				}
			}
		});
		
		subnetEditor.addListenerToApplyButton(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int index = listSubnets.getSelectedIndex();
				if (index != -1) {
					DhcpSubnet subnet = subnetEditor.getSubnet();
					DhcpSubnet oldSubnet = (DhcpSubnet) configuration.getElementAt(index);
					subnet.setClients(oldSubnet.getClients());
					configuration.remove(index);
					configuration.add(index, subnet);
					listSubnets.setSelectedValue(subnet, true);
				}
			}
		});
	}
	
	public DhcpConfiguration getConfiguration() {
		return configuration;
	}
}
